package interpreter.bytecodes;

import interpreter.VirtualMachine;
import java.util.NoSuchElementException;
import java.util.Vector;
import javax.swing.*;

/**
 *
 * @author Kristofer Selbekk
 */
public class ReadByteCode extends ByteCode {
    private int value;

    /**
     * Initializes the byte code with no arguments
     */
    public void init( Vector<String> args ) {
       
    }

    /**
     * Action: Reads in number from user
     *
     * Note: Swing elements will be used here to ease the user experience.
     *
     * @param rt the Virtual Machine reference
     */
    public void exec( VirtualMachine rt ) {
        String inStr = JOptionPane.showInputDialog("Please enter an integer");
        boolean cont = true;

        // Retries to read in data until correct input
        while (cont) {
            try {
                value = Integer.parseInt( inStr );
                cont = false;
            } catch( NumberFormatException nfe ) {
                // Do nothing, but try again
                cont = true;
            }
        }

        // Puts on top of stack
        rt.pushOnStack(value);
    }

    public Object getArgs() {
        return null;
    }
}